<?php
/**
 * Panx 登记的所有设备型号
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/6/7
 * @copyright PanxSoft Inc.
 */
namespace App\Models\Panx;

use App\Models\Customer\DeviceAccount;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * @property int id
 * @property string device_name
 * @property string specification
 * @property SkinDetector skinDetector
 * @property DeviceSpec deviceSpec
 * @property mixed deviceAccounts
 * @property mixed count
 *
 * @method self filterUuid($uuid) - deprecated
 * @method self filterDeviceName($device_name)
 * @method self filterSpecification($specification)
 * @method self filterProducer($producer)
 * @method self filterCode($code)
 * @method self filterDeviceCode()
 *
 * @mixin Builder
 */
class Device extends Model
{
    protected $table = 'devices';

    protected $guarded = [];

    /**
     * @deprecated - v2.3.3 后肤质检测仪多对多关联到设备账号
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function skinDetector()
    {
        return $this->belongsTo(SkinDetector::class, 'skin_detector_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function deviceSpec()
    {
        return $this->belongsTo(DeviceSpec::class, 'device_spec_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function deviceAccounts()
    {
        return $this->hasMany(DeviceAccount::class, 'device_id', 'id');
    }

    /**
     * @deprecated
     *
     * @used-by filterUuid
     *
     * @param Builder $query
     * @param $uuid
     * @return Builder
     */
    public function scopeFilterUuid(Builder $query, $uuid)
    {
        if (! $uuid) return $query;

        return $query->where('uuid','like', '%'. $uuid.'%');
    }

    /**
     * @used-by filterDeviceName
     *
     * @param Builder $query
     * @param $device_name
     * @return Builder
     */
    public function scopeFilterDeviceName(Builder $query, $device_name)
    {
        if (! $device_name) return $query;

        return $query->where('device_name','like', '%'. $device_name.'%');
    }

    /**
     * @used-by filterSpecification
     *
     * @param Builder $query
     * @param $specification
     * @return Builder
     */
    public function scopeFilterSpecification(Builder $query, $specification)
    {
        if (! $specification) return $query;

        return $query->where('specification','like', '%'. $specification.'%');
    }

    /**
     * @used-by filterProducer
     *
     * @param Builder $query
     * @param $producer
     * @return Builder
     */
    public function scopeFilterProducer(Builder $query, $producer)
    {
        if (! $producer) return $query;

        return $query->where('producer','like', '%'. $producer.'%');
    }

    /**
     * @used-by filterCode
     *
     * @param Builder $query
     * @param $code
     * @return Builder
     */
    public function scopeFilterCode(Builder $query, $code)
    {
        if (! $code) return $query;

        return $query->where('code', 'like', '%'.$code.'%');
    }

    /**
     * @used-by filterDeviceCode
     *
     * @param Builder $query
     * @return Builder
     */
    public function scopeFilterDeviceCode(Builder $query)
    {
        return $query->where('code', 'PB02W')
            ->orWhere('code', 'PMOM2')
            ->orWhere('code', 'PB1803I')
            ->orWhere('code', 'PMRS2')
            ->orWhere('code', 'PMCS3');
    }
}
